package models;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Rafael Milléo
 */
public class Cliente {
    
    private int id;
    private String nome;
    private String endereco;
    private String telefone;
    
    public Cliente(int id){
        this.load(id);
    }
    
    public Cliente(){}

    public boolean insert(){
        String queryString = "INSERT INTO Clientes (nome, endereco, telefone) VALUES ";
        queryString += "('" + this.nome + "', '" + this.endereco + "', '" + this.telefone + "')";
        
        Database db = new Database();
        db.connect();
        int clienteId = db.executeUpdate(queryString);
        db.disconnect();
        
        this.setId(clienteId);
        
        return true;
    }
    public boolean delete(){
        Database db = new Database();
        db.connect();
        
        String queryString = "DELETE FROM Clientes WHERE id_clientes = " + this.getId();
        db.executeUpdate(queryString);
        
        db.disconnect();
        
        return true;
    }
    public boolean update(){
        if(this.getId() != 0){
            Database db = new Database();
            db.connect();

            String queryString = "UPDATE Clientes SET nome = '" + this.nome +
                    "' endereco = '" + this.endereco +
                    "' telefone = '" + this.telefone +
                    "' WHERE id_clientes = " + this.id;
            
            db.executeUpdate(queryString);
            db.disconnect();
            
            return true;
        }else{ 
            return false;
        }
    }
    public void load(int id){
        this.setId(id);
        this.load();
    }
    public void load(String nome){
        this.setNome(nome);
        this.load();
    }
    
    private void load(){
        String sqlQuery = "";
        if(this.getId() != 0){
            sqlQuery = "SELECT * FROM Clientes WHERE id_cliente = " + this.getId() + " LIMIT 1";
        }else{
            if(!this.getNome().isEmpty()){
                sqlQuery = "SELECT * FROM Clientes WHERE nome = '" + this.getNome() + "' LIMIT 1";
            }
        }
        
        Database db = new Database();
        db.connect();
        db.query(sqlQuery);
        
        try{
            db.res.next();
            this.setId(db.res.getInt("id_cliente"));
            this.setNome(db.res.getString("nome"));
            this.setEndereco(db.res.getString("endereco"));
            this.setTelefone(db.res.getString("telefone"));
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            db.disconnect();
        }
    }
    
    public static ResultSet listAll(){
        Database db = new Database();
        db.connect();
        db.query("SELECT * FROM Clientes");
        db.disconnect();
        
        return db.res;
    }
    
    public static String[] listAllAsArray(){
        Database db = new Database();
        String[] clientList;
        db.connect();
        
        int rowCount = 0;
                
        try {
            db.query("SELECT count(*) FROM Clientes");
            db.res.next();
            rowCount = db.res.getInt(1);
            
            db.query("SELECT nome FROM Clientes ORDER BY nome");
        
            clientList = new String[rowCount];
            for(int i = 0; i < rowCount; i++){
                db.res.next();
                clientList[i] = db.res.getString("nome");
            }

            db.disconnect();
            
            return clientList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    /**
     * @return the nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * @param nome the nome to set
     */
    public void setNome(String nome) {
        this.nome = nome;
    }

    /**
     * @return the endereco
     */
    public String getEndereco() {
        return endereco;
    }

    /**
     * @param endereco the endereco to set
     */
    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    /**
     * @return the telefone
     */
    public String getTelefone() {
        return telefone;
    }

    /**
     * @param telefone the telefone to set
     */
    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
    
}
